package com.cal.calendar;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Display;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.GridView;

public class MonthGridView extends GridView {

	private Context monthContext;
	/**
	 * 每行的列数,初始化为7列
	 */
	private static int COLUMN_NUM = 7;
	/**
	 * 网格之间的距离,初始化为1px
	 */
	private static int SPACE = 1;

	public MonthGridView(Context context) {

		super(context);

		this.monthContext = context;

		setGridViewCommonAttr();
	}

	private void setGridViewCommonAttr() {

		// 设置对齐格式,水平完全填充,垂直自动适应
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);

		setLayoutParams(params);
		// 设置每行7列
		setNumColumns(COLUMN_NUM);
		// 设置内容水平居中
		setGravity(Gravity.CLIP_VERTICAL);
		// 设置水平间距和垂直间距
		setHorizontalSpacing(SPACE);
		setVerticalSpacing(SPACE);

		// 设置背景色
		setBackgroundColor(Color.LTGRAY);

		// 获取窗口管理器
		WindowManager windowManager = ((Activity) monthContext)
				.getWindowManager();
		// 获取显示窗口的信息
		Display display = windowManager.getDefaultDisplay();

		/**
		 * 设置网格视图的左边填充区域大小,来规避屏幕宽度无法分成@COLUMN_NUM份的问题
		 */
		int columnWidth = display.getWidth() / COLUMN_NUM;
		int excessWidth = display.getWidth() - columnWidth * COLUMN_NUM;
		int leftPadding = excessWidth / 2;

		setPadding(leftPadding, 0, 0, 0);
	}
}
